package com.yss.acs.redisdemo;

import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;

/**
 * @auther lvwenming
 * @date 2022/3/2 9:52
 */
public class redistest {
    public static void main(String[] args) {
        teststring();
        System.out.println("-------------------------");
        testpool();
        System.out.println("-------------------------");
        testSentinel();
        System.out.println("-------------------------");
        testCluster();
    }

    private static void testCluster() {
        JedisCluster cluster = getCluster();
        cluster.set("username","zhangsan");
        String username = cluster.get("username");
        System.out.println(username);
        cluster.close();
    }


    private static void testSentinel() {
        Jedis jedis = getSentinelPool();
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
        closejedis(jedis);
    }

    public static void teststring() {
        Jedis jedis = getjedis();
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
        closejedis(jedis);
    }

    public static void testpool() {
        Jedis jedis = getJedispoll();
        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
        closejedis(jedis);
    }

    public static JedisPool jedisPool;

    public static Jedis getJedispoll() {
        if (jedisPool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(10);
            config.setMaxIdle(5);
            config.setMinIdle(5);
            config.setBlockWhenExhausted(true);
            config.setTestOnBorrow(true);
            config.setMaxWaitMillis(2000);


            jedisPool = new JedisPool(config, "hadoop001", 6379);
        }
        return jedisPool.getResource();
    }

    public static void closejedis(Jedis jedis) {
        if (jedis != null)
            jedis.close();
    }

    public static Jedis getjedis() {
        Jedis jedis = new Jedis("hadoop001", 6379);
        return jedis;
    }

    public static JedisSentinelPool sentinelpool = null;

    public static Jedis getSentinelPool() {
        if (sentinelpool == null) {
            Set<String> sentinelSet = new HashSet<>();
            sentinelSet.add("192.168.17.102:26379");
            sentinelSet.add("192.168.17.103:26379");

            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(10);
            config.setMaxIdle(5);
            config.setMinIdle(5);
            config.setBlockWhenExhausted(true);
            config.setTestOnBorrow(true);
            config.setMaxWaitMillis(2000);

            sentinelpool = new JedisSentinelPool("mymaster", sentinelSet, config);
        }
        return sentinelpool.getResource();
    }

    public static JedisCluster jedisCluster = null;

    private static JedisCluster getCluster() {
        if (jedisCluster == null) {
            Set<HostAndPort> clusterSet = new HashSet<>();
            clusterSet.add(new HostAndPort("192.168.17.101", 6379));
            clusterSet.add(new HostAndPort("192.168.17.102", 6379));
            clusterSet.add(new HostAndPort("192.168.17.103", 6379));

            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(10);
            config.setMaxIdle(5);
            config.setMinIdle(5);
            config.setBlockWhenExhausted(true);
            config.setTestOnBorrow(true);
            config.setMaxWaitMillis(2000);

            jedisCluster = new JedisCluster(clusterSet,config);
        }
        return jedisCluster;
    }
}
